

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script
src="https://maps.googleapis.com/maps/api/js?language=en?key=YOUR_API_KEY"> <!-- Assigned to my Google account -->
<!-- Look closely. Path? -->
</script>
</head>
<body>
<div id="map-canvas"></div>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: 50.671441, lng: -120.36273}, // TRU
zoom: 14
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
| Create | HTTP POST | |
| Read | HTTP GET | |
| Update | HTTP PUT | Not supported in HTML5 |
| Delete | HTTP DELETE | Not supported in HTML5 |
/tokens/{id}, not /getToken/tokens or /tokens/{id}/value for a singleton resouce/tokens, /tokens/{id}, /tokens/{id}/value/tokens/{id1},{id2}/tokens?username=David/tokens?username=David-Williams, not /Tokens?Username=David_Williams/tokens/
| Create | HTTP POST | |
| Read | HTTP GET | |
| Update | HTTP PUT | Not supported in HTML5 |
| Delete | HTTP DELETE | Not supported in HTML5 |
/tokens/{id}, not /getToken/tokens or /tokens/{id}/value for a singleton resouce/tokens, /tokens/{id}, /tokens/{id}/value/tokens/{id1},{id2}/tokens?username=David/tokens?username=David-Williams, not /Tokens?Username=David_Williams/tokens/POST /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then error??? /collections?tokenid=...&name=...'{"collectionid":"...", "explanaton":"..."}', where if collectionid is negative, then error??? /collections/{collection_id}?tokenid=...&document=..., where the document value should be a JSON string.
'{"result":"true"|"false", "explanation":"..."}'??? /collections/{collection_id}?tokenid=...&query=..., where the query value should be a JSON string for MongoDB query.
'{"result":"true"|"false", "documents":"...", "explanation":"..."}', where the document value is a JSON string of an array of found documents.??? /collections/{collection_id}?tokenid=...&query=...&document=...'{"result":"true"|"false", "explanation":"..."}'??? /collections/{collection_id}?tokenid=...&query=...'{"result":"true"|"false", "explanation":"..."}'??? /tokens/{tokenid}'{"result":"true"|"false", "explanation":"..."}'??? /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'var db = new TRUMongoDBService(host) - Connect to the server .open().collection().close()db.register(username, password, callback(result)) - Register a new userdb.open(username, password, callback(result)) - Sign indb.collection(collection_name, callback(result)) - Select a collectiondb.insert(document, callback(result)),
db.find(query, callback(result)),
db.update(query, document, callback(result))
db.delete(query, callback(result)),
db.close() - Close the connectiontoken_id and collection_id are not included in the above APIapp.METHOD(PATH, HANDLER)
const express = require('express')
const app = express()
const port = 8087
// Allow Cross-domain requests, i.e., CORS (Cross-Origin Resource Sharing)
// Why do we need this?
app.all('/*', function(req, res, next) { // /*: any routes
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); // Default: only GET and POST
next(); // Express middleware function; To continue to next operations
});
// GET method route; what is a router here?
app.get('/', (req, res) => {
res.send('GET: Hello World!')
})
// POST method route
app.post('/', (req, res) => {
res.send('POST: Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
const express = require('express')
const app = express()
const bodyParser = require('body-parser') // for query with POST
const port = 8087
// For CORS
...
// GET method route; what is a router here?
app.get('/users', (req, res) => {
res.send('GET: username=' + req.query.username + "; password=" + ????)
})
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// POST method route
app.post('/users', (req, res) => {
res.send('POST: username=' + req.body.username + "; password=" + ????)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
req.params.id.
Here is an example.
const express = require('express')
const app = express()
const port = 8087
// For CORS
...
// GET method route; what is a router here?
app.get('/tokens/:id', (req, res) => {
res.send('GET: /tokens; id=" + req.params.id);
})
// POST method route
app.post('/tokens/:id', (req, res) => {
res.send('POST: /tokens; id=" + req.params.id);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
ready(cb)
true or false
close()
isValid(username, password, cb)
true or false
register(username, password, cb)
true or false
unsubscribe(username)
getNewToken(username, cb)
token id, or
-1 (error case)
deleteToken(id, cb)
true or false
collection(token_id, collection_name, cb) // use collection_name for token_id
(true, 1), or
(false, -1) or (false, -2)
insertOne(token_id, doc, cb)
find(token_id, query, cb)
(true, result) or
false
updateMany(token_id, query, newdoc, cb)
true or false
deleteMany(token_id, query, cb)
true or false
var express = require('express');
var bodyParser = require('body-parser');
var model = require('./model.js');
var app = express();
var server = app.listen(8087, function () {
var host = server.address().address;
var port = server.address().port;
console.log("Listening at http://%s:%s", host, port)
})
// for POST query
app.use(bodyParser.urlencoded({ extended: true }));
// To support CORS
app.all('/*', function(req, res, next) { // /*: any routes
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); // Default: only GET and POST
next(); // Express middleware function; To continue to next operations
});
// Route operations
app.get('/', function (req, res) { // HTTP GET method
res.send("Welcome to TRU MongoDB Web Service!");
})
// SignUp
// Request - POST /users?username=...&password=...
// Response - '{"result":"true"|"false", "explanation":"..."}'
app.post('/users', function (req, res) { // HTTP POST method
var username = req.body.username;
var password = req.body.password;
console.log("username = %s, password = %s", username, password);
model.ready(function(result) {
if (result) {
model.register(username, password, function(result) {
if (result)
res.send(JSON.stringify({result:true, explanation:""}));
else
res.send(JSON.stringify({result:false, explanation:"Registration error"}));
});
}
else
res.send(JSON.stringify({result:false, explanation:"Connection error"}));
});
});
// ???
app.post(path/:id, function(req, res) { // Several different post with different paths
var id = req.params.id;
....
});
// ???
app.get(path, function(req, res) {
var id = req.query.id;
....
});
// ???
app.put(path, function(req, res) { // HTTP PUT method
var doc = req.body.document
....
});
// ???
app.delete(path, function(req, res) { .... }); // HTTP DELETE method
...
??? /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /????username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then error??? /???/{tokenid}'{"result":"true"|"false", "explanation":"..."}'var db = new TRUMongoDBWebService(host) - Connect to the server .open().collection().close()db.register(host, username, password, function(result) {...}) - Register a new userdb.unsubscribe(host, username, password, function(result) {...}) - Delete a userdb.open(host, username, password, function(result) {...}) - Sign indb.collection(collection_name, function(result) {...}) - Select a collectiondb.insert(document, function(result) {...}), // document and query are JSON strings of objects
db.find(query, function(result) {...}), // result is an object
db.update(query, document, function(result) {...})
db.delete(query, function(result) {...}),
db.close() - Close the connectiontoken_id and collection_id are not included in the above APITRUMongoDBWebService()
function TRUMongoDBWebService() {
this.host = ""; // with open()
this.connection_token_id = -1;
this.collection_name = "";
this.collection_id = -1;
this.register = function(host, username, password, callback) {
...
}
this.unsubscribe = function(host, username, password, callback) {
...
}
...
}
register()
this.register = function(host, u, p, callback) {
$.ajax({
url: host + "/users",
method: "post",
data: { username: u, password: p },
success: function(data) {
data = JSON.parse(data);
callback(data);
}
});
}
POST /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'DELETE /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'POST /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then errorDELETE /tokens/{tokenid}'{"result":"true"|"false", "explanation":"..."}'